package com.iamteer.cupband;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import it.sephiroth.android.wheel.view.Wheel;

public class GoalSettingActivity extends Activity {

    private ImageView ivBack;
    private ImageView ivGoalSetting;
    WiperSwitch swGoalSetting;
    private Wheel mWheel;
    private TextView tvGoal;
    private TextView tvEnableGoalSetting;
    private LinearLayout goalSettingLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goal_setting);
//        Log.i("TAG", "GoalSettingActivity Create");

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View titleView = inflater.inflate(R.layout.activity_goal_setting_title, null);
        actionBar.setCustomView(titleView, lp);

        actionBar.setDisplayShowHomeEnabled(false);//去掉导航
        actionBar.setDisplayShowTitleEnabled(false);//去掉标题
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);

        ImageView ivBack = (ImageView) actionBar.getCustomView().findViewById(R.id.ivBack);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mWheel = (Wheel) findViewById(R.id.wheel);
        tvGoal = (TextView) findViewById(R.id.tvGoal);
        swGoalSetting = (WiperSwitch) findViewById(R.id.swGoalSetting);
        goalSettingLayout = (LinearLayout) findViewById(R.id.goalSettingLayout);
        tvEnableGoalSetting = (TextView) findViewById(R.id.tvEnableGoalSetting);

        swGoalSetting.setChecked(MainActivity.goalSettingEnable);

        if(swGoalSetting.nowStatus){
            goalSettingLayout.setEnabled(true);
            goalSettingLayout.setActivated(true);
            goalSettingLayout.setVisibility(View.VISIBLE);
            tvEnableGoalSetting.setText("开");
        } else {
            goalSettingLayout.setEnabled(false);
            goalSettingLayout.setActivated(false);
            goalSettingLayout.setVisibility(View.INVISIBLE);
            tvEnableGoalSetting.setText("关");
        }
//        tvGoal.setText(Integer.toString(MainActivity.Goal));
//        mWheel.setValue((MainActivity.Goal - 8) / 16f, true);
//        System.out.println(Float.toString((MainActivity.Goal - 8) / 16f));

        swGoalSetting.setOnChangedListener(new WiperSwitch.OnChangedListener() {
            @Override
            public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
                MainActivity.goalSettingEnable = checkState;
                if (checkState) {
                    goalSettingLayout.setEnabled(true);
                    goalSettingLayout.setActivated(true);
                    goalSettingLayout.setVisibility(View.VISIBLE);
                    tvEnableGoalSetting.setText("开");

                } else {
                    goalSettingLayout.setEnabled(false);
                    goalSettingLayout.setActivated(false);
                    goalSettingLayout.setVisibility(View.INVISIBLE);
                    tvEnableGoalSetting.setText("关");
                }
                SharedPreferences sp = getSharedPreferences(MainActivity.SharedPreferencesName, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean(MainActivity.KEY_goalSettingEnable, checkState);
                editor.apply();
            }
        });

        mWheel.setOnLayoutListener(new Wheel.OnLayoutListener() {
            @Override
            public void onLayout(View view) {
                tvGoal.setText(Integer.toString(MainActivity.Goal));
                mWheel.setValue((MainActivity.Goal - 8) / 8f, true);
//                System.out.println("MainActivity.Goal: " + MainActivity.Goal);
//                System.out.println("onLayout: "+Float.toString((MainActivity.Goal - 8) / 8f));
            }
        });

        mWheel.setOnScrollListener(new Wheel.OnScrollListener() {

            @Override
            public void onScrollStarted(Wheel view, float value, int roundValue) {

                tvGoal.setText(Integer.toString(roundValue+8));
            }

            @Override
            public void onScrollFinished(Wheel view, float value, int roundValue) {
                if (value == -1) {
                    Toast.makeText(GoalSettingActivity.this, "目标不能小于0!", Toast.LENGTH_SHORT).show();
                }else if(value == 1){
                    Toast.makeText(GoalSettingActivity.this, "喝水太多也不好哦!", Toast.LENGTH_SHORT).show();
                }
                tvGoal.setText(Integer.toString(roundValue+8));
                MainActivity.Goal = roundValue+8;
                SharedPreferences sp = getSharedPreferences(MainActivity.SharedPreferencesName, Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putInt(MainActivity.KEY_Goal, roundValue + 8);
                editor.apply();
                System.out.println(String.format("Old: %d %d %d %f %f", mWheel.getWheelScaleFactor(), mWheel.getTicksCount(), mWheel.getTicks(), mWheel.getTickSpace(), mWheel.getValue()));
            }

            @Override
            public void onScroll(Wheel view, float value, int roundValue) {
                tvGoal.setText(Integer.toString(roundValue + 8));
            }
        });
    }

///*
//    @Override
//    protected void onStart() {
//        super.onStart();
//        Log.i("TAG", "GoalSettingActivity Start");
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        Log.i("TAG", "GoalSettingActivity Resume");
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        Log.i("TAG", "GoalSettingActivity Pause");
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        Log.i("TAG", "GoalSettingActivity Stop");
//    }
//
//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        Log.i("TAG", "GoalSettingActivity Restart");
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        Log.i("TAG", "GoalSettingActivity Destroy");
//    }*/
}
